package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduChapterDTO;
import com.zenith.edu.po.vo.EduChapterVO;
import com.zenith.edu.service.EduChapterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/chapter")
@Tag(name = "章节管理", description = "章节管理接口")
public class EduChapterController {
    
    @Autowired
    private EduChapterService eduChapterService;
    
    @SecurePostMapping(value = "/createChapter",roles = {"ADMIN"},permission = "chapter:create")
    @Operation(summary = "创建章节")
    public Result<EduChapterVO> createChapter(@RequestBody EduChapterDTO dto) {
        EduChapterVO chapterVO = eduChapterService.createChapter(dto);
        return Result.success(chapterVO);
    }
    
    @SecurePostMapping(value = "/getChapterById/{id}",roles = "ROLE_ADMIN",permission = "role:read")
    @Operation(summary = "根据ID获取章节详情")
    public Result<EduChapterVO> getChapterById(@PathVariable Long id) {
        EduChapterVO chapterVO = eduChapterService.getChapterById(id);
        if (chapterVO == null) {
            return Result.fail("章节不存在");
        }
        return Result.success(chapterVO);
    }
    
    @SecurePostMapping(value = "/updateChapter",roles = {"ADMIN"},permission = "chapter:update")
    @Operation(summary = "更新章节信息")
    public Result<EduChapterVO> updateChapter(@RequestBody EduChapterDTO dto) {
        EduChapterVO chapterVO = eduChapterService.updateChapter(dto);
        return Result.success(chapterVO);
    }
    
    @SecurePostMapping(value = "/deleteChapter/{id}",roles = {"ADMIN"},permission = "chapter:delete")
    @Operation(summary = "删除指定章节")
    public Result<Void> deleteChapter(@PathVariable Long id) {
        boolean result = eduChapterService.deleteChapter(id);
        if (!result) {
            return Result.fail("章节删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/list",roles = {"ADMIN"},permission = "chapter:read")
    @Operation(summary = "获取章节列表")
    public Result<List<EduChapterVO>> getChapterList() {
        List<EduChapterVO> chapterList = eduChapterService.getChapterList();
        return Result.success(chapterList);
    }
    
    @SecurePostMapping(value = "/getChaptersByCourseId/{courseId}",roles = "ROLE_ADMIN",permission = "role:read")
    @Operation(summary = "根据课程ID获取章节列表")
    public Result<List<EduChapterVO>> getChaptersByCourseId(@PathVariable Long courseId) {
        List<EduChapterVO> chapterList = eduChapterService.getChaptersByCourseId(courseId);
        return Result.success(chapterList);
    }
}